/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.oa.web;

import com.alibaba.druid.support.json.JSONUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.JacksonUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ExportExcelSheet;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.web.HttpOutMessage;
import com.thinkgem.jeesite.common.web.OutMessageUtils;
import com.thinkgem.jeesite.modules.act.entity.Act;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.oa.entity.Budget;
import com.thinkgem.jeesite.modules.oa.entity.BudgetBatch;
import com.thinkgem.jeesite.modules.oa.entity.Subject;
import com.thinkgem.jeesite.modules.oa.entity.TestAudit;
import com.thinkgem.jeesite.modules.oa.entity.export.ElectricityFeeDetail;
import com.thinkgem.jeesite.modules.oa.service.BudgetService;
import com.thinkgem.jeesite.modules.oa.service.SubjectService;
import com.thinkgem.jeesite.modules.oa.service.TestAuditService;
import com.thinkgem.jeesite.modules.sys.dao.RoleDao;
import com.thinkgem.jeesite.modules.sys.entity.Category;
import com.thinkgem.jeesite.modules.sys.entity.Classify;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.CategoryService;
import com.thinkgem.jeesite.modules.sys.service.ClassifyService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 审批Controller
 * @author thinkgem
 * @version 2014-05-16
 */
@Controller
@RequestMapping(value = "${adminPath}/oa/budget")
public class BudgetController extends BaseController {
	ObjectMapper mapper = new ObjectMapper();

	@Autowired
	private CategoryService categoryService;
	@Autowired
	private BudgetService budgetService;
	@Autowired
	private SubjectService subjectService;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private ClassifyService classifyService;
	@Autowired
	private ActTaskService actTaskService;
	@ModelAttribute
	public Budget get(@RequestParam(required=false) String id){//,
//			@RequestParam(value="act.procInsId", required=false) String procInsId) {
		Budget budget = null;
		if (StringUtils.isNotBlank(id)){
			budget = budgetService.get(id);
//		}else if (StringUtils.isNotBlank(procInsId)){
//			testAudit = testAuditService.getByProcInsId(procInsId);
		}
		if (budget == null){
			budget = new Budget();
		}
		return budget;
	}
	
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = {"list", ""})
	public String list(Budget budget, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		budget.setCreateBy(user);
		if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(budget.getAnnual())){
			budget.setAnnual(DateUtils.getNextYear());
		}
        Page<Budget> page = budgetService.findPage(new Page<Budget>(request, response), budget);
		for(Budget budget1:page.getList()){
			List<Act> histoicFlowList = actTaskService.histoicFlowList(budget1.getAct().getProcInsId(), "", "");
			if(histoicFlowList != null && histoicFlowList.size()>0){
				budget1.setCurrentAct(histoicFlowList.get(histoicFlowList.size()-1));
			}
		}
		model.addAttribute("page", page);
		return "modules/oa/budgetList";
	}

	/**
	 * 预算列表，根据用户的角色data_scope判断获取的预算列表级别
	 * @param budget
	 * @param request
	 * @param response
	 * @param model
     * @return
     */
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = {"listBudget"})
	public String getBudgetList(Budget budget, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		String dataScope = roleDao.getMinDataScopeByUserId(user.getId());
		List<Subject> subjects = subjectService.findList(new Subject());
		Page<Budget> page = new Page<Budget>(request, response);
		if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(budget.getAnnual())){
			budget.setAnnual(DateUtils.getNextYear());
		}
		double budgetSum = 0;
		if (dataScope == null){
			page.setList(new ArrayList<Budget>());
			budgetSum = 0;
		}else if (dataScope.equals(Role.DATA_SCOPE_ALL)){//系统管理员可查看所有用户的预算申请
			budgetService.findPage(page,budget);
			budgetSum = Double.parseDouble(page.getValue1()+"");
		}else if (dataScope.equals(Role.DATA_SCOPE_OFFICE_AND_CHILD)){//部门及其子部门用户的预算申请
			budgetService.getOfficeAndBelowBudgetList(page,user.getOffice().getId(),budget);
			budgetSum = Double.parseDouble(page.getValue1()+"");
		}else if (dataScope.equals(Role.DATA_SCOPE_OFFICE)){//查看该部门用户的预算申请，不包括子部门
			budgetService.getOfficeBudgetList(page,user.getOffice().getId(),budget);
			budgetSum = Double.parseDouble(page.getValue1()+"");
		}else if (dataScope.equals(Role.DATA_SCOPE_SELF)){//个人只能查看自己发布的预算申请
			budgetService.getBudgetListByCreateUser(page,user.getId(),budget);
			budgetSum = Double.parseDouble(page.getValue1()+"");
		}else {//其他情况，返回一个空的list
			page.setList(new ArrayList<Budget>());
			budgetSum = 0;
		}
		for(Budget budget1:page.getList()){
			List<Act> histoicFlowList = actTaskService.histoicFlowList(budget1.getAct().getProcInsId(), "", "");
			if(histoicFlowList != null && histoicFlowList.size()>0){
				budget1.setCurrentAct(histoicFlowList.get(histoicFlowList.size()-1));
			}
		}
		model.addAttribute("budgetSum",budgetSum);
		model.addAttribute("page", page);
		model.addAttribute("subjects",subjects);
		return "modules/oa/budgetList2";
	}
	/**
	 * 预算列表，根据用户的角色data_scope判断获取的预算列表级别
	 * @param budget
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = {"listBudgetEdit"})
	public String getBudgetListEdit(Budget budget, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(budget.getAnnual())){
			budget.setAnnual(DateUtils.getNextYear());
		}
		String dataScope = roleDao.getMinDataScopeByUserId(user.getId());
		List<Subject> subjects = subjectService.findList(new Subject());
		Page<Budget> page = new Page<Budget>(request, response);
		if (dataScope == null){
			page.setList(new ArrayList<Budget>());
		}else if (dataScope.equals(Role.DATA_SCOPE_ALL)){//系统管理员可查看所有用户的预算申请
			budgetService.findPage(page,budget);
		}else if (dataScope.equals(Role.DATA_SCOPE_OFFICE_AND_CHILD)){//部门及其子部门用户的预算申请
			budgetService.getOfficeAndBelowBudgetList(page,user.getOffice().getId(),budget);
		}else if (dataScope.equals(Role.DATA_SCOPE_OFFICE)){//查看该部门用户的预算申请，不包括子部门
			budgetService.getOfficeBudgetList(page,user.getOffice().getId(),budget);
		}else if (dataScope.equals(Role.DATA_SCOPE_SELF)){//个人只能查看自己发布的预算申请
			budgetService.getBudgetListByCreateUser(page,user.getId(),budget);
		}else {//其他情况，返回一个空的list
			page.setList(new ArrayList<Budget>());
		}
		model.addAttribute("page", page);
		model.addAttribute("subjects",subjects);
		return  "modules/oa/budgetListEdit";
	}
	@RequestMapping(value = {"findAggregateGroupByClassify"})
	@ResponseBody
	public HttpOutMessage findAggregateGroupByClassify(Budget budget){
		List<Budget> budgetAggregate = budgetService.findAggregateGroupByClassify(budget);
		HttpOutMessage outMessage = OutMessageUtils.buildOutMessage(HttpOutMessage.SUCCESS,budgetAggregate);
		return outMessage;
	}
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = {"exportBudget"})
	public String exportBudget(Budget budget, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		User user = UserUtils.getUser();
		/*if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(budget.getAnnual())){
			budget.setAnnual(DateUtils.getYear());
		}*/
		String dataScope = roleDao.getMinDataScopeByUserId(user.getId());
		List<Subject> subjects = subjectService.findList(new Subject());
		Page<Budget> page = new Page<Budget>(request, response);
		page.setPageSize(-1);
		if (dataScope == null){
			page.setList(new ArrayList<Budget>());
		}else if (dataScope.equals(Role.DATA_SCOPE_ALL)){//系统管理员可查看所有用户的预算申请
			budgetService.findPage(page,budget);
		}else if (dataScope.equals(Role.DATA_SCOPE_OFFICE_AND_CHILD)){//部门及其子部门用户的预算申请
			budgetService.getOfficeAndBelowBudgetList(page,user.getOffice().getId(),budget);
		}else if (dataScope.equals(Role.DATA_SCOPE_OFFICE)){//查看该部门用户的预算申请，不包括子部门
			budgetService.getOfficeBudgetList(page,user.getOffice().getId(),budget);
		}else if (dataScope.equals(Role.DATA_SCOPE_SELF)){//个人只能查看自己发布的预算申请
			budgetService.getBudgetListByCreateUser(page,user.getId(),budget);
		}else {//其他情况，返回一个空的list
			page.setList(new ArrayList<Budget>());
		}
		try {
			String fileName = "预算填报表"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			List<String> headList = new ArrayList<String>();
			headList.add("项目名称");
			headList.add("所属处室");
			headList.add("项目类型");
			headList.add("项目分类");
			headList.add("总计");
			for(Subject subject:subjects){
				headList.add("["+subject.getCode()+"]"+subject.getName());
			}
//			ExportExcel excel = new ExportExcel("预算填报表", headList).setDataList(page.getList()).write(response, fileName).dispose();
			ExportExcel excel = new ExportExcel("预算填报表", headList);
			for(Budget b: page.getList()){
				Row row = excel.addRow();
				excel.addCell(row,0,b.getTitle());
				excel.addCell(row,1,b.getOffice());
				excel.addCell(row,2,b.getClassify());
				excel.addCell(row,3,b.getCategory());
				excel.addCell(row,4,b.getAggregate());
				int i = 5;
				for(Subject s:b.getSubjectList()){
					excel.addCell(row,i++,s.getMoney());
				}
			}
			excel.write(response,fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出预算填报表失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/oa/budget/listBudgetEdit?repage";
	}
	/**
	 * 申请单批量保存/修改
	 * @param budgetBatch
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("oa:budget:edit")
	@RequestMapping(value = "saveBatch")
	public String saveBatch(BudgetBatch budgetBatch, Model model, RedirectAttributes redirectAttributes) {
		budgetService.saveBudgetSubjectBatch(budgetBatch);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + adminPath + "/oa/budget/listBudgetEdit";
	}

	/**
	 * 三级项目统计
	 * @param budget
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = {"listBudgetStatistics"})
	public String getBudgetListStatistics(Budget budget, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(budget.getAnnual())){
			budget.setAnnual(DateUtils.getNextYear());
		}
		Category category = new Category();
		category.setId("202309477e0d486cb03fa602f4d9c5aa");
		List<Category> categories = categoryService.findListByParentId(category);//查询三级项目类别
		List<Subject> subjectsAll = subjectService.findList(new Subject());
		double sum = 0;
		for(Category c:categories){
			double total = 0;
			budget.setCategory(c);
			List<Subject> subjects = budgetService.findThirdLevelStatistics(budget);
			if(subjects != null && subjects.size()>0){
				c.setSubjects(subjects);
				for(Subject s:subjects){
					total += Double.parseDouble(s.getMoney());
				}
			}else {
				c.setSubjects(subjectsAll);
			}
			c.setTotal(total);
			sum += total;
		}
		for(int i=0;i<subjectsAll.size();i++){
			double sum2 = 0;
			for(Category c:categories){
				Subject s = c.getSubjects().get(i);
				sum2 += Double.parseDouble(s.getMoney());
			}
			subjectsAll.get(i).setSum(sum2+"");
		}
		model.addAttribute("sum",sum);
		model.addAttribute("categories",categories);
		model.addAttribute("subjects",subjectsAll);//查询预算项目
		return  "modules/oa/budgetListStatistic";
	}
	/**
	 * 导出数据
	 * @param budget
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = "export", method=RequestMethod.POST)
	public String exportFile(Budget budget, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "业务维持费测算明细"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(budget.getAnnual())){
				budget.setAnnual(DateUtils.getNextYear());
			}
			List<Budget> budgets = budgetService.findBudgetListDetail(budget);
			ExportExcelSheet excel = new ExportExcelSheet();
			Sheet pressFeeDetailSheet = excel.addSheet("印刷费明细","印刷费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","印刷品名称","印刷依据","用途","彩色印刷页数","彩色印刷标准","黑白印刷页数","黑白印刷标准","单价(万元/本、份)","总金额","印刷标准文件(黑白、彩印)","定价依据","数量依据"));
			Sheet consultingFeeDetailSheet = excel.addSheet("咨询费明细","咨询费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","专家类别","天数(次)","邀请人数(会议咨询)","邀请人数(通信咨询)","会议咨询标准(万元)","通讯咨询标准(万元)","总金额(万元)","与项目任务的关系","专家咨询内容","聘请专家费用标准依据","专家人数依据","地点(交通费)","招待费"));
			Sheet electricityFeeDetailSheet = excel.addSheet("电费明细","电费明细",excel.createHeaderList("序号","项目名称","使用设备情况","数量(度)","单价(万元)","金额(万元)","设备耗电量(功率、设备运行时间)"));
			Sheet postFeeDetailSheet = excel.addSheet("邮电费明细","邮电费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","数量","单价(万元)","金额(万元)","备注","邮寄费"));
			Sheet travelFeeDetailSheet = excel.addSheet("差旅费明细","差旅费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","人数","标准(万元)","金额(万元)","与项目关系","地点、路线、工作内容","定价标准依据","人数依据"));
			Sheet internationalFeeDetailSheet = excel.addSheet("国际合作交流费明细","国际合作交流费明细",excel.createHeaderList("序号","项目名称","出国任务名称","类别","国家","交通费标准(万元)","交通费人数","交通费金额(万元)","食宿费标准(万元)","食宿费人数","食宿费天数","食宿费金额(万元)","公杂费标准(万元)","公杂费人数","公杂费金额(万元)","总金额(万元)","内容说明","人数依据","费用标准依据"));
			Sheet maintainFeeDetailSheet = excel.addSheet("维修(护)费明细","维修(护)费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","维修(护)内容","金额(万元)","与项目关系(万元)","定价依据","每年维护次数，是否每年进行研究，以前年度如何（必要性）"));
			Sheet rentFeeDetailSheet = excel.addSheet("租赁费明细","租赁费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","现有设备情况","租赁需求情况","金额(万元)","与项目关系","定价依据","租赁期限"));
			Sheet trainingFeeDetailSheet = excel.addSheet("培训费明细","培训费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","培训任务名称","是否本单位组织","天数","参加人数","标准(万元/天)","金额","培训内容说明","与项目任务关系","定价依据","人数依据","培训地点"));
			Sheet materialFeeDetailSheet = excel.addSheet("专用材料费明细","专用材料费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","名称","单价","数量","金额(万元)","用途","定价依据","数量依据"));
			Sheet stationMaterialFeeDetailSheet = excel.addSheet("站网专用材料费明细","站网专用材料费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","名称","单价","数量","金额(万元)","用途","定价依据","数量依据"));
			Sheet labMaterialFeeDetailSheet = excel.addSheet("实验室专用材料费明细","实验室专用材料费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","名称","单价","数量","金额(万元)","用途","定价依据","数量依据"));
			Sheet laborMaterialFeeDetailSheet = excel.addSheet("劳务费明细","劳务费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","人数","月数","发放标准(万元)","金额(万元)","与项目关系","发放说明","定价依据","数量依据"));
			Sheet delegateMaterialFeeDetailSheet = excel.addSheet("委托业务费明细","委托业务费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","委托业务名称","金额(万元)","内容说明","用途","测算依据"));
			Sheet otherTrafficFeeDetailSheet = excel.addSheet("其它交通费明细","其它交通费明细",excel.createHeaderList("序号","项目名称","财政部下达项目","人数","标准(万元)","金额(万元)","与项目关系","地点、路线、工作内容","定价标准依据","人数依据"));
			Sheet fundmentalFeeDetailSheet = excel.addSheet("业务基础设备(办公设备)购置明细","业务基础设备(办公设备)购置明细",excel.createHeaderList("序号","项目名称","设备名称","设备分类","设备型号","单价(万元/台)","数量(台、件)","总金额(万元)","内容说明","购置设备生产国别与地区","用途","定价依据","数量依据"));
			Sheet specialFundmentalFeeDetailSheet = excel.addSheet("专用设备购置明细","专用设备购置明细",excel.createHeaderList("序号","项目名称","设备名称","设备分类","设备型号","单价(万元/台)","数量(台、件)","总金额(万元)","内容说明","购置设备生产国别与地区","用途","定价依据","数量依据"));
			Sheet officeFundmentalFeeDetailSheet = excel.addSheet("业务办公耗材购置明细","业务办公耗材购置明细",excel.createHeaderList("序号","项目名称","设备名称","设备分类","设备型号","单价(万元/台)","数量(台、件)","总金额(万元)","内容说明","购置设备生产国别与地区","用途","定价依据","数量依据"));
			Sheet informationFeeDetailSheet = excel.addSheet("信息网络及软件购置明细","信息网络及软件购置明细",excel.createHeaderList("序号","项目名称","设备名称","设备分类","设备型号","单价(万元/台)","数量(台、件)","总金额(万元)","主要技术性能指标","购置设备生产国别与地区","用途","定价依据","数量依据"));

			for(Budget b: budgets){
				List<Subject> subjects = b.getSubjectList();
				for(Subject s:subjects){
					if("30202".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(pressFeeDetailSheet);
							excel.addCell(row, 0, pressFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("pressWorkName"));
							excel.addCell(row, 4, detail.get("pressAccording"));
							excel.addCell(row, 5, detail.get("purpose"));
							excel.addCell(row, 6, detail.get("colorPressNumber"));
							excel.addCell(row, 7, detail.get("colorPressStandard"));
							excel.addCell(row, 8, detail.get("blackWhitePressNumber"));
							excel.addCell(row, 9, detail.get("blackWhitePressStandard"));
							excel.addCell(row, 10, detail.get("price"));
							excel.addCell(row, 11, detail.get("number"));
							excel.addCell(row, 12, detail.get("money"));
							excel.addCell(row, 13, detail.get("pressStandardDoc"));
							excel.addCell(row, 14, detail.get("priceDepend"));
							excel.addCell(row, 15, detail.get("numberDepend"));
						}
					}else if("30203".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(consultingFeeDetailSheet);
							excel.addCell(row, 0, consultingFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, DictUtils.getDictLabel(detail.get("specialistType"),"specialist_fee","1"));
							excel.addCell(row, 4, detail.get("days"));
							excel.addCell(row, 5, detail.get("invitePersonMeeting"));
							excel.addCell(row, 6, detail.get("invitePersonCommu"));
							excel.addCell(row, 7, detail.get("meetingStandard2"));
							excel.addCell(row, 8, detail.get("commuStandard"));
							excel.addCell(row, 9, detail.get("money"));
							excel.addCell(row, 10, detail.get("relation"));
							excel.addCell(row, 11, detail.get("content"));
							excel.addCell(row, 12, detail.get("priceStandard"));
							excel.addCell(row, 13, detail.get("numberStandard"));
							excel.addCell(row, 14, detail.get("address"));
							excel.addCell(row, 15, detail.get("entertainmentExpense"));
						}
					}else if("30206".equals(s.getCode())){
						String electricityFeeDetailJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(electricityFeeDetailJson))
							continue; ;
						electricityFeeDetailJson = electricityFeeDetailJson.replaceAll("&quot;","\"");
						List<Map<String,String>> electricityFeeDetails = mapper.readValue(electricityFeeDetailJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> electricityFeeDetail:electricityFeeDetails) {
							Row row = excel.addRow(electricityFeeDetailSheet);
							excel.addCell(row, 0, electricityFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, electricityFeeDetail.get("type"));
							excel.addCell(row, 3, Integer.parseInt(electricityFeeDetail.get("number")));
							excel.addCell(row, 4, Double.parseDouble(electricityFeeDetail.get("price")));
							excel.addCell(row, 5, Double.parseDouble(electricityFeeDetail.get("money")));
							excel.addCell(row, 6, electricityFeeDetail.get("consume"));
						}
					}else if("30207".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(postFeeDetailSheet);
							excel.addCell(row, 0, postFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("number"));
							excel.addCell(row, 4, detail.get("price"));
							excel.addCell(row, 5, detail.get("money"));
							excel.addCell(row, 6, detail.get("remark"));
							excel.addCell(row, 7, detail.get("postMoney"));
						}
					}else if("30210".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(travelFeeDetailSheet);
							excel.addCell(row, 0, travelFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("number"));
							excel.addCell(row, 4, detail.get("price"));
							excel.addCell(row, 5, detail.get("money"));
							excel.addCell(row, 6, detail.get("relation"));
							excel.addCell(row, 7, detail.get("content"));
							excel.addCell(row, 8, detail.get("priceDepend"));
							excel.addCell(row, 9, detail.get("numberDepend"));
						}
					}else if("30211".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(internationalFeeDetailSheet);
							excel.addCell(row, 0, internationalFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, detail.get("taskName"));
							excel.addCell(row, 3, detail.get("type"));
							excel.addCell(row, 4, detail.get("country"));
							excel.addCell(row, 5, detail.get("trafficFeeStandard"));
							excel.addCell(row, 6, detail.get("trafficFeeCount"));
							excel.addCell(row, 7, detail.get("trafficFee"));
							excel.addCell(row, 8, detail.get("accommodationFeeStandard"));
							excel.addCell(row, 9, detail.get("accommodationFeeCount"));
							excel.addCell(row, 10, detail.get("accommodationFeeDays"));
							excel.addCell(row, 11, detail.get("accommodationFee"));
							excel.addCell(row, 12, detail.get("incidentalsFeeStandard"));
							excel.addCell(row, 13, detail.get("incidentalsFeeCount"));
							excel.addCell(row, 14, detail.get("incidentalsFee"));
							excel.addCell(row, 15, detail.get("money"));
							excel.addCell(row, 16, detail.get("content"));
							excel.addCell(row, 17, detail.get("priceDepend"));
							excel.addCell(row, 18, detail.get("numberDepend"));
						}
					}else if("30212".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(maintainFeeDetailSheet);
							excel.addCell(row, 0, maintainFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("content"));
							excel.addCell(row, 4, detail.get("money"));
							excel.addCell(row, 5, detail.get("relation"));
							excel.addCell(row, 6, detail.get("priceDepend"));
							excel.addCell(row, 7, detail.get("numberDepend"));
						}
					}else if("30213".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(rentFeeDetailSheet);
							excel.addCell(row, 0, rentFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("condition"));
							excel.addCell(row, 4, detail.get("conditionDemond"));
							excel.addCell(row, 5, detail.get("money"));
							excel.addCell(row, 6, detail.get("relation"));
							excel.addCell(row, 7, detail.get("priceDepend"));
							excel.addCell(row, 8, detail.get("period"));
						}
					}else if("30215".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(trainingFeeDetailSheet);
							excel.addCell(row, 0, trainingFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("taskName"));
							excel.addCell(row, 4, detail.get("isOurArrange"));
							excel.addCell(row, 5, detail.get("count"));
							excel.addCell(row, 6, detail.get("number"));
							excel.addCell(row, 7, detail.get("price"));
							excel.addCell(row, 8, detail.get("money"));
							excel.addCell(row, 9, detail.get("content"));
							excel.addCell(row, 10, detail.get("relation"));
							excel.addCell(row, 11, detail.get("priceDepend"));
							excel.addCell(row, 12, detail.get("numberDepend"));
							excel.addCell(row, 13, detail.get("address"));
						}
					}else if("30217".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(materialFeeDetailSheet);
							excel.addCell(row, 0, materialFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("name"));
							excel.addCell(row, 4, detail.get("price"));
							excel.addCell(row, 5, detail.get("number"));
							excel.addCell(row, 6, detail.get("money"));
							excel.addCell(row, 7, detail.get("purpose"));
							excel.addCell(row, 8, detail.get("priceDepend"));
							excel.addCell(row, 9, detail.get("numberDepend"));
						}
					}else if("30218".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(stationMaterialFeeDetailSheet);
							excel.addCell(row, 0, stationMaterialFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("name"));
							excel.addCell(row, 4, detail.get("price"));
							excel.addCell(row, 5, detail.get("number"));
							excel.addCell(row, 6, detail.get("money"));
							excel.addCell(row, 7, detail.get("purpose"));
							excel.addCell(row, 8, detail.get("priceDepend"));
							excel.addCell(row, 9, detail.get("numberDepend"));
						}
					}else if("30219".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(labMaterialFeeDetailSheet);
							excel.addCell(row, 0, labMaterialFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("name"));
							excel.addCell(row, 4, detail.get("price"));
							excel.addCell(row, 5, detail.get("number"));
							excel.addCell(row, 6, detail.get("money"));
							excel.addCell(row, 7, detail.get("purpose"));
							excel.addCell(row, 8, detail.get("priceDepend"));
							excel.addCell(row, 9, detail.get("numberDepend"));
						}
					}else if("30220".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(laborMaterialFeeDetailSheet);
							excel.addCell(row, 0, laborMaterialFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("count"));
							excel.addCell(row, 4, detail.get("number"));
							excel.addCell(row, 5, detail.get("price"));
							excel.addCell(row, 6, detail.get("money"));
							excel.addCell(row, 7, detail.get("relation"));
							excel.addCell(row, 8, detail.get("explain"));
							excel.addCell(row, 9, detail.get("priceDepend"));
							excel.addCell(row, 10, detail.get("numberDepend"));
						}
					}else if("30221".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(delegateMaterialFeeDetailSheet);
							excel.addCell(row, 0, delegateMaterialFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("name"));
							excel.addCell(row, 4, detail.get("money"));
							excel.addCell(row, 5, detail.get("content"));
							excel.addCell(row, 6, detail.get("purpose"));
							excel.addCell(row, 7, detail.get("depend"));
						}
					}else if("30225".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(otherTrafficFeeDetailSheet);
							excel.addCell(row, 0, otherTrafficFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, b.getClassify().getName());
							excel.addCell(row, 3, detail.get("number"));
							excel.addCell(row, 4, detail.get("price"));
							excel.addCell(row, 5, detail.get("money"));
							excel.addCell(row, 6, detail.get("relation"));
							excel.addCell(row, 7, detail.get("content"));
							excel.addCell(row, 8, detail.get("priceDepend"));
							excel.addCell(row, 9, detail.get("numberDepend"));
						}
					}else if("30229".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(fundmentalFeeDetailSheet);
							excel.addCell(row, 0, fundmentalFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, detail.get("instrumentName"));
							excel.addCell(row, 3, detail.get("instrumentCategory"));
							excel.addCell(row, 4, detail.get("instrumentType"));
							excel.addCell(row, 5, detail.get("price"));
							excel.addCell(row, 6, detail.get("number"));
							excel.addCell(row, 7, detail.get("money"));
							excel.addCell(row, 8, detail.get("content"));
							excel.addCell(row, 9, detail.get("address"));
							excel.addCell(row, 10, detail.get("purpose"));
							excel.addCell(row, 11, detail.get("priceDepend"));
							excel.addCell(row, 12, detail.get("numberDepend"));
						}
					}else if("30230".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(specialFundmentalFeeDetailSheet);
							excel.addCell(row, 0, specialFundmentalFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, detail.get("instrumentName"));
							excel.addCell(row, 3, detail.get("instrumentCategory"));
							excel.addCell(row, 4, detail.get("instrumentType"));
							excel.addCell(row, 5, detail.get("price"));
							excel.addCell(row, 6, detail.get("number"));
							excel.addCell(row, 7, detail.get("money"));
							excel.addCell(row, 8, detail.get("content"));
							excel.addCell(row, 9, detail.get("address"));
							excel.addCell(row, 10, detail.get("purpose"));
							excel.addCell(row, 11, detail.get("priceDepend"));
							excel.addCell(row, 12, detail.get("numberDepend"));
						}
					}else if("30231".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(officeFundmentalFeeDetailSheet);
							excel.addCell(row, 0, officeFundmentalFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, detail.get("instrumentName"));
							excel.addCell(row, 3, detail.get("instrumentCategory"));
							excel.addCell(row, 4, detail.get("instrumentType"));
							excel.addCell(row, 5, detail.get("price"));
							excel.addCell(row, 6, detail.get("number"));
							excel.addCell(row, 7, detail.get("money"));
							excel.addCell(row, 8, detail.get("content"));
							excel.addCell(row, 9, detail.get("address"));
							excel.addCell(row, 10, detail.get("purpose"));
							excel.addCell(row, 11, detail.get("priceDepend"));
							excel.addCell(row, 12, detail.get("numberDepend"));
						}
					}else if("30233".equals(s.getCode())){
						String detailsJson = s.getDetails();
						if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(detailsJson))
							continue; ;
						detailsJson = detailsJson.replaceAll("&quot;","\"");
						List<Map<String,String>> details = mapper.readValue(detailsJson,new TypeReference<List<Map<String,String>>>() { });
						for(Map<String,String> detail:details) {
							Row row = excel.addRow(informationFeeDetailSheet);
							excel.addCell(row, 0, informationFeeDetailSheet.getLastRowNum()-1);
							excel.addCell(row, 1, b.getTitle());
							excel.addCell(row, 2, detail.get("instrumentName"));
							excel.addCell(row, 3, detail.get("instrumentCategory"));
							excel.addCell(row, 4, detail.get("instrumentType"));
							excel.addCell(row, 5, detail.get("price"));
							excel.addCell(row, 6, detail.get("number"));
							excel.addCell(row, 7, detail.get("money"));
							excel.addCell(row, 8, detail.get("content"));
							excel.addCell(row, 9, detail.get("address"));
							excel.addCell(row, 10, detail.get("purpose"));
							excel.addCell(row, 11, detail.get("priceDepend"));
							excel.addCell(row, 12, detail.get("numberDepend"));
						}
					}
				}
			}
			excel.write(response,fileName).dispose();
			return null;
		} catch (Exception e) {
			logger.error("导出业务维持费测算明细异常:",e);
			addMessage(redirectAttributes, "导出业务维持费测算明细！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/oa/budget/listBudgetStatistics?repage";
	}
	/**
	 * 上报统计
	 * @param budget
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = {"listBudgetStatistics2"})
	public String getBudgetListStatistics2(Budget budget, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(com.thinkgem.jeesite.common.utils.StringUtils.isBlank(budget.getAnnual())){
			budget.setAnnual(DateUtils.getNextYear());
		}
		List<Classify> retClassifies = new ArrayList<Classify>();
		Classify classify = new Classify();
		classify.setParentIds("0,a795355bce8745039ef570f3a6567737,202309477e0d486cb03fa602f4d9c5aa,");
		List<Classify> classifies1 = classifyService.findList(classify);
		retClassifies.addAll(classifies1);
		classify.setParentIds("0,a795355bce8745039ef570f3a6567737,17765757e8fb497683c23b9105ab8ef5,");
		List<Classify> classifies2 = classifyService.findList(classify);
		retClassifies.addAll(classifies2);
		List<Subject> subjectsAll = subjectService.findList(new Subject());

		double classify1Sum = 0;
		double classify1BudgetMoneyTotal = 0;
		double classify2Sum = 0;
		double classify2BudgetMoneyTotal = 0;
		for(Classify c:classifies1){
			double total = 0;
			budget.setClassify(c);
			List<Subject> subjects = budgetService.findClassifyStatistics(budget);
			if(subjects != null && subjects.size()>0){
				c.setSubjects(subjects);
				for(Subject s:subjects){
					total += Double.parseDouble(s.getMoney());
				}
			}else {
				c.setSubjects(subjectsAll);
			}
			c.setTotal(total);
			classify1Sum += total;
			classify1BudgetMoneyTotal += Double.parseDouble(c.getBudgetMoney());
		}
		for(Classify c:classifies2){
			double total = 0;
			budget.setClassify(c);
			List<Subject> subjects = budgetService.findClassifyStatistics(budget);
			if(subjects != null && subjects.size()>0){
				c.setSubjects(subjects);
				for(Subject s:subjects){
					total += Double.parseDouble(s.getMoney());
				}
			}else {
				c.setSubjects(subjectsAll);
			}
			c.setTotal(total);
			classify2Sum += total;
			classify2BudgetMoneyTotal += Double.parseDouble(c.getBudgetMoney());
		}
		List<Map<String,Double>> classifySubjectSum = new ArrayList<Map<String, Double>>();
		for(int i=0;i<subjectsAll.size();i++){
			double sum1 = 0;
			for(Classify c:classifies1){
				Subject s = c.getSubjects().get(i);
				sum1 += Double.parseDouble(s.getMoney());
			}
			double sum2 = 0;
			for(Classify c:classifies2){
				Subject s = c.getSubjects().get(i);
				sum2 += Double.parseDouble(s.getMoney());
			}
			Map<String,Double> map = new HashedMap();
			map.put("sum1",sum1);
			map.put("sum2",sum2);
			map.put("sum",sum1+sum2);
			classifySubjectSum.add(map);
		}
		double sum = classify1Sum+classify2Sum;//申请总总和
		double budgetMoneyTotal = classify1BudgetMoneyTotal + classify2BudgetMoneyTotal;//预算总金额
		model.addAttribute("sum", sum);
		model.addAttribute("budgetMoneyTotal",budgetMoneyTotal);

		model.addAttribute("classify1Sum", classify1Sum);
		model.addAttribute("classify1BudgetMoneyTotal",classify1BudgetMoneyTotal);

		model.addAttribute("classify2Sum", classify2Sum);
		model.addAttribute("classify2BudgetMoneyTotal",classify2BudgetMoneyTotal);

//		model.addAttribute("classifies",retClassifies);
		model.addAttribute("classifies1",classifies1);
		model.addAttribute("classifies2",classifies2);
		model.addAttribute("subjects",subjectsAll);//查询预算项目
		model.addAttribute("classifySubjectSum",classifySubjectSum);//查询预算项目

		return  "modules/oa/budgetListStatistic2";
	}
	/**
	 * 申请单填写
	 * @param budget
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = "processedTask")
	public String processedTask(Budget budget, Model model) {
		String view = "budgetView";
		budget = budgetService.getByProcInsId(budget.getProcInsId());
		List<Subject> subjectList = subjectService.findList(new Subject());
		// 查看审批申请单
		if (StringUtils.isNotBlank(budget.getId())){//.getAct().getProcInsId())){
			subjectList = budgetService.findBudgetSubject(budget);
		}
		budget.setSubjectList(subjectList);
		model.addAttribute("budget", budget);
		return "modules/oa/" + view;
	}
	/**
	 * 申请单填写
	 * @param budget
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:budget:view")
	@RequestMapping(value = "form")
	public String form(Budget budget, Model model) {
		
		String view = "budgetForm";
		List<Subject> subjectList = subjectService.findList(new Subject());
		// 查看审批申请单
		if (StringUtils.isNotBlank(budget.getId())){//.getAct().getProcInsId())){
			subjectList = budgetService.findBudgetSubject(budget);
			// 环节编号
			String taskDefKey = budget.getAct().getTaskDefKey();
			
			// 查看工单
			if(budget.getAct().isFinishTask()){
				view = "budgetView";
			}
			// 修改环节
			else if ("modify".equals(taskDefKey)){
				view = "budgetForm";
			}

			// 审核环节
			else if ("audit".equals(taskDefKey)){
				view = "budgetAudit";
//				String formKey = "/oa/testAudit";
//				return "redirect:" + ActUtils.getFormUrl(formKey, testAudit.getAct());
			}
			// 审核环节2
			else if ("audit2".equals(taskDefKey)){
				view = "budgetAudit";
			}
		}
		budget.setSubjectList(subjectList);
		model.addAttribute("budget", budget);
		return "modules/oa/" + view;
	}
    /**
     * 申请单历史记录
     * @param budget
     * @param model
     * @return
     */
    @RequiresPermissions("oa:budget:view")
    @RequestMapping(value = "history")
    public String history(Budget budget, Model model) {
        List<Subject> subjectList = budgetService.findBudgetSubjectHistroy(budget);
        Double aggregate = 0.0;
		if(subjectList != null && subjectList.size()>0){
			for(Subject s:subjectList){
				String money = s.getMoney();
				aggregate += Double.parseDouble(money);
			}
		}
		budget.setAggregate(aggregate+"");
		budget.setSubjectList(subjectList);
        model.addAttribute("budget", budget);
        return "modules/oa/budgetHistory";
    }
    /**
     * 申请单修改
     * @param budget
     * @param model
     * @return
     */
    @RequiresPermissions("oa:budget:edit")
    @RequestMapping(value = "modify")
    public String modify(Budget budget, Model model) {
        List<Subject> subjectList = budgetService.findBudgetSubject(budget);
        budget.setSubjectList(subjectList);
        model.addAttribute("budget", budget);
        return "modules/oa/budgetForm";
    }
	/**
	 * 申请单保存/修改
	 * @param budget
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("oa:budget:edit")
	@RequestMapping(value = "save")
	public String save(Budget budget, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, budget)){
			return form(budget, model);
		}
		budgetService.save(budget);
		addMessage(redirectAttributes, "提交成功");
//		return "redirect:" + adminPath + "/act/task/todo/";
        return "redirect:" + adminPath + "/oa/budget/";
	}

	/**
	 * 工单执行（完成任务）
	 * @param budget
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:budget:edit")
	@RequestMapping(value = "saveBudget")
	public String saveAudit(Budget budget, Model model) {
		if (StringUtils.isBlank(budget.getAct().getFlag())
				|| StringUtils.isBlank(budget.getAct().getComment())){
			addMessage(model, "请填写审核意见。");
			return form(budget, model);
		}
		budgetService.auditSave(budget);
		return "redirect:" + adminPath + "/act/task/todo/";
	}
	@RequiresPermissions("oa:budget:edit")
	@RequestMapping(value = "saveBudgetBatch")
	@ResponseBody
	public String saveAuditBatch(String[] procInsIds,String[] taskIds,String[] taskDefKeys,String type, Model model) {
		if(taskIds != null && taskIds.length>0){
			for(int i=0;i<taskIds.length;i++){
				Budget budget = budgetService.getByProcInsId(procInsIds[i]);
				if("yes".equals(type)){
					budget.getAct().setFlag("yes");
				}else {
					budget.getAct().setFlag("no");
				}
				budget.getAct().setTaskId(taskIds[i]);
				budget.getAct().setTaskDefKey(taskDefKeys[i]);
				budgetService.auditSave(budget);
			}
		}
		return "true";//adminPath + "/act/task";
	}
	/**
	 * 删除工单
	 * @param budget
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("oa:budget:edit")
	@RequestMapping(value = "delete")
	public String delete(Budget budget, RedirectAttributes redirectAttributes) {
		budgetService.delete(budget);
		addMessage(redirectAttributes, "删除审批成功");
		return "redirect:" + adminPath + "/oa/budget/?repage";
	}

}
